using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using GameFramework.MathLibraries;
using GameFramework.Graphics;

namespace GameFramework.Objects
{
    public enum ActiveAnimationType
    {
        None,
        Rotate,
        Zoom,
    }
    public class TransitionFrameModule : ThreePointObject
    {
        private const float EXTRA_ZOOM = 0.15f;

        private ActiveAnimationType _activeAnimationType = ActiveAnimationType.None;
        public ActiveAnimationType ActiveAnimationType
        {
            get
            {
                return _activeAnimationType;
            }
            set
            {
                _activeAnimationType = value;
            }
        }

        FrameModule _framemodule;
        public FrameModule FrameModule
        {
            get
            {
                return _framemodule;
            }
            set
            {
                _framemodule = value;
            }
        }

        public bool EnableOpacity = false;
   

        #region Events
        public event EventHandler Hidden;
        #endregion

        /// <summary>
        /// Default constructor, do not use mid point
        /// </summary>
        /// <param name="game"></param>
        /// <param name="onPosition"></param>
        /// <param name="activePosition"></param>
        /// <param name="offPosition"></param>
        public TransitionFrameModule(Game game, Vector2 onPosition, Vector2 activePosition, Vector2 offPosition, FrameModule frame) 
            : base(game, onPosition,activePosition,offPosition)
        {
            _framemodule = frame;
        }

        public override void BeginActiveTransition()
        {
            _framemodule.Rotate = 0;
            base.BeginActiveTransition();
        }

        public override void OnActiveTransition()
        {
            switch(_activeAnimationType)
            {
                case ActiveAnimationType.None:
                    break;
                case ActiveAnimationType.Rotate:
                    _framemodule.Rotate = (float)(-2 * Math.PI * TransitionAlpha);
                    break;
                case ActiveAnimationType.Zoom:
                    float d = 1+ (float)Math.Sin(TransitionAlpha * Math.PI)*EXTRA_ZOOM;
                    _framemodule.Scale = d;    
                    break;                
            }
            base.OnActiveTransition();
        }

        public override void EndActiveTransition()
        {
            _framemodule.Rotate = 0;           

            base.EndActiveTransition();
        }

        public override void EndTransitionOff()
        {
            if (Hidden != null)
                Hidden(this, new EventArgs());
            base.EndTransitionOff();
        }

        /// <summary>
        /// Use the mid point
        /// </summary>
        /// <param name="game"></param>
        /// <param name="onPosition"></param>
        /// <param name="offPosition"></param>
        public TransitionFrameModule(Game game, Vector2 onPosition, Vector2 offPosition,FrameModule frame) 
            :base(game, onPosition, MathLibraries.MathLibraries.MidPoint(onPosition,offPosition), offPosition)
        {
            _framemodule = frame;
        }

        public override void Draw(GameTime gameTime)
        {
            if (!Visible)
                return;
            if (EnableOpacity)
            {
                if (CurrentState == TransitionState.TransitionOn)
                {
                    byte value = (byte)((1.0f - TransitionAlpha) * 255);
                    _framemodule.Draw(Position, 1, 0, new Color(value, value, value, value));
                }
            }
            else
            {
                _framemodule.Draw(Position);  
            }
                                      
        }        
    }
}
